package com.legend.utils;

import org.apache.log4j.Logger;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Request{
    static Logger log = Logger.getRootLogger();
	/**
	 * 获取参数值
	 * 
	 * @param request
	 * @param paramName  参数名
	 * @return
	 */
	public static String getString(HttpServletRequest request, String paramName) {
		String value = request.getParameter(paramName);
		if (value == null) {
			return "";
		}
		return value;
	}

    /**
     * 获取post data数据
     * @param request
     * @return
     */
    public static String getData(HttpServletRequest request) {
        String data = "";
        try {
            ServletInputStream in = request.getInputStream();
            InputStreamReader reader = new InputStreamReader(in, "utf-8");
            BufferedReader buffer = new BufferedReader(reader);
            String line;
            while ((line = buffer.readLine()) != null) {
                System.out.printf(line);
                data += line;
            }
            buffer.close();
            reader.close();
            in.close();
        }catch (IOException e)
        {
            log.error(e.getMessage());
        }
        return data;
    }
	/**
	 * 获取整型值
	 * 
	 * @param request
	 * @param paramName
	 *            参数名
	 * @param defaultVal
	 *            默认值
	 * @return
	 */
	public static Integer getInteger(HttpServletRequest request, String paramName, Integer defaultVal) {
		String value = request.getParameter(paramName);
		if (value == null) {
			return defaultVal;
		}
		return Integer.valueOf(value);
	}
}
